﻿@using Abp.Extensions
@using FutureGoodsItem = FutureApp.Shop.GoodsItem
@using FutureGoodsBrand = FutureApp.Shop.GoodsBrand

@section Scripts{
    <script>
        !function ($) {

            var $$$ = function () {
                this.$box = $('.box');
                this.$table = $('.table');
                this.service = abp.services.app.goods;
                this.localize = abp.localization.localize;
            };
            $$$.prototype = {
                init: function () {
                    this.binding();
                    this.initTable();
                },
                initTable: function () {
                    var self = this;
                    self.$table.bootstrapTable({
                        toggle: "table",
                        striped: true,
                        showColumns: true,
                        showRefresh: true,
                        pagination: true,
                        keyEvents: true,
                        sortable: false,
                        advancedSearch: true,
                        idTable: "advancedTable",
                        sidePagination: 'server',
                        pageNumber: 1,
                        pageSize: 10,
                        pageList: [10, 25, 50, 100, 200],
                        dataField: "items",
                        totalField: "totalCount",
                        queryParams: function (params) {
                            var result = {
                                skipCount: params.offset,
                                maxResultCount: params.offset + params.limit
                            };
                            return $.extend(result, params.predicate || {});
                        },
                        ajax: function (params) {
                            abp.ui.setBusy(self.$box);
                            self.service.getGoodsList(params.data)
						        .done(params.success)
                                .fail(params.error)
						        .always(function () {
						            abp.ui.clearBusy(self.$box);
						        });
                            return params;
                        },
                        uniqueId: "id",
                        columns: [
                            {
                                field: '@("SerialNo".ToCamelCase())',
                                title: self.localize("GoodsSerialNo")
                            },
                            {
                                field: '@("Name".ToCamelCase())',
                                title: self.localize("GoodsName")
                            },
                            {
                                field: '@("BrandName".ToCamelCase())',
                                title: self.localize("GoodsBrandName"),
                                formatter: function(_, row){ return row.name; }
                            },
                            {
                                field: '@("MouldName".ToCamelCase())',
                                title: self.localize("GoodsMouldName"),
                                formatter: function(_, row){ return row.name; }
                            },
                            {
                                field: '@("CategoryName".ToCamelCase())',
                                title: self.localize("GoodsCategoryName"),
                                formatter: function(_, row){ return row.name; }
                            },
                            {
                                title: self.localize("Operation"),
                                searchable: false,
                                formatter: function (value, row, index) {
                                    var html = '<a class="btn btn-primary btn-xs" href="javascript:;" data-action="edit">@L("Edit")</a>';
                                    html += ' ';
                                    if (row.isActive) {
                                        html += '<a class="btn btn-danger btn-xs" href="javascript:;" data-action="disable">@L("GoodsDisable")</a>';
                                    } else {
                                        html += '<a class="btn btn-success btn-xs" href="javascript:;" data-action="enable">@L("GoodsEnable")</a>';
                                    }
                                    return html;
                                },
                            }
                        ],
                        searchs: [
                            {
                                field: '@("SerialNo".ToCamelCase())',
                                length: @FutureGoodsItem.MaxSerialNoLength,
                                title: self.localize("GoodsSerialNo")
                            },
                            {
                                field: '@("Name".ToCamelCase())',
                                length: @FutureGoodsItem.MaxNameLength,
                                title: self.localize("GoodsName")
                            },
                            {
                                field: '@("BrandName".ToCamelCase())',
                                length: @FutureGoodsBrand.MaxNameLength,
                                title: self.localize("GoodsBrandName")
                            },
                            {
                                field: '@("MouldName".ToCamelCase())',
                                length: @FutureGoodsBrand.MaxNameLength,
                                title: self.localize("GoodsMouldName")
                            },
                            {
                                field: '@("CategoryName".ToCamelCase())',
                                length: @FutureGoodsBrand.MaxNameLength,
                                title: self.localize("GoodsCategoryName")
                            }
                        ]
                    });
                },
                binding: function () {
                    var self = this;
                    this.$box.on("click", ".btn", function (e) {
                        var $sender = $(e.currentTarget);
                        var action = $.trim($sender.data("action"));
                        var gid = $sender.parents("tr").data("uniqueid");
                        switch (action) {
                            case "edit":
                                self.editGoods(gid);
                                break;
                            case "disable":
                                self.disableGoods(gid);
                                break;
                            case "enable":
                                self.enableGoods(gid);
                                break;
                        }
                    });
                },
                editGoods: function (gid) {
                    var html = abp.utils.formatString('<form action="{0}" method="get"><input type="hidden" name="id" value="{1}" /></form>', "@Url.Action("Edit")", gid);
                    $(html).submit();
                },
                enableGoods: function (gid) {
                    var self = this;
                    abp.message.confirm(self.localize("AskEnableGoods"))
                        .done(function (isConfirm) {
                            if (!isConfirm) return;
                            abp.ui.setBusy(self.$box);
                            abp.timing.wait(100).done(function () {
                                self.service.enableGoods(gid).done(function () {
                                    abp.message.success(self.localize("EnableGoodsSucceed"))
                                        .done(function () { location.reload(true); });
                                }).always(function () {
                                    abp.ui.clearBusy(self.$box);
                                });
                            });
                        });
                },
                disableGoods: function (gid) {
                    var self = this;
                    abp.message.confirm(self.localize("AskDisableGoods"))
                        .done(function (isConfirm) {
                            if (!isConfirm) return;
                            abp.ui.setBusy(self.$box);
                            abp.timing.wait(100).done(function () {
                                self.service.disableGoods(gid).done(function () {
                                    abp.message.success(self.localize("DisableGoodsSucceed"))
                                        .done(function () { location.reload(true); });
                                }).always(function () {
                                    abp.ui.clearBusy(self.$box);
                                });
                            });
                        });
                }
            };

            $(function () { (new $$$()).init(); });
        }(jQuery);
    </script>
}

<ul class="content-nav">
    <li class="active">
        <a href="javascript:;">@L("List")</a>
    </li>
    <li>
        <a href="@Url.Action("add")">@L("Add")</a>
    </li>
</ul>
<div class="box box-success box-edge-all" data-dock="true">
    <table class="table"></table>
</div>